package com.neuedu.service.impl;

import com.neuedu.dao.AddFangchanNumberDao;
import com.neuedu.dao.HouseServiceDao;

import com.neuedu.dao.impl.AddFangchangNumberDaoImpl;
import com.neuedu.dao.impl.AddXiaoquNumberDaoImpl;
import com.neuedu.dao.impl.HouseServiceDaoImpl;
import com.neuedu.model.AddFangchanNumber;
import com.neuedu.model.AddXiaoquNumber;
import com.neuedu.model.FangChan;
import com.neuedu.service.HouseService;
import com.neuedu.vo.PageVO;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class HouseServiceImpl implements HouseService {
    private HouseServiceDao houseServiceDao=new HouseServiceDaoImpl();
    private AddFangchanNumberDao fangchanNumber= new AddFangchangNumberDaoImpl();
    @Override
    public PageVO<FangChan> selectxianshi(String minDate, String maxDate, String likeName, Integer pageNo, Integer pageSize) {
        Integer limitParams1=(pageNo-1)*pageSize;
        Integer limitParams2=pageSize;
        List<FangChan> house = houseServiceDao.selectPage(minDate, maxDate, likeName, limitParams1, limitParams2);
        PageVO<FangChan> pageVO=new PageVO<>();
        pageVO.setPageNo(pageNo);
        pageVO.setPageSize(pageSize);
        pageVO.setData(house);
        //还差  pageCount页的总数  itemCount数据的总数
        Integer itemCount=houseServiceDao.count(minDate, maxDate, likeName);
        Integer pageCount=((itemCount%pageSize)==0)?(itemCount/pageSize):(itemCount/pageSize+1);
        pageVO.setItemCount(itemCount);
        pageVO.setPageCount(pageCount);
        return pageVO;
    }

    @Override
    public int addHouse(FangChan fangChan) {
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);


        AddFangchanNumber addFangchanNumber = fangchanNumber.select();
        String number="XY";
        number=number+format.replace("-","");
        if(addFangchanNumber.getNumberTime().equals(format)){
            if(addFangchanNumber.getNumber()<10){
                number=number+"00"+addFangchanNumber.getNumber();
            }else if(addFangchanNumber.getNumber()<100){
                number=number+"0"+addFangchanNumber.getNumber();
            }else{
                number=number+addFangchanNumber.getNumber();
            }
            addFangchanNumber.setNumber(addFangchanNumber.getNumber()+1);
            fangchanNumber.qudata(addFangchanNumber);
        }else{
            addFangchanNumber.setNumberTime(format);
            addFangchanNumber.setNumber(1);
            fangchanNumber.qudata(addFangchanNumber);
            number=number+"001";
        }
        fangChan.setFangchanbianma(number);
        return houseServiceDao.insertFanChan(fangChan);
    }

    @Override
    public int shanchuHouse(Integer[] ids) {
        return houseServiceDao.delFanchan(ids);
    }


}
